<?php
class v1_role{
    function index(){
    	$role=$this->yum->model('role');
    	$data['total']=$role->Count();
    	$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$data['role']=$role->GRole(array('where'=>array('locked !='=>10),'limit'=>array(intval($this->yum->get('start')),15)));
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$this->yum->config['Manage']['domain'].'index.php?c=role&a=index','total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function append(){
    	if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$name=$this->yum->post('name',true);$describe=$this->yum->post('describe',true);$locked=intval($this->yum->post('locked',true));
			if(strlen($name)<=0){
				$output['msg']='名称不可为空.';
			}else{
				$role=$this->yum->model('role');
				$temp=$role->GRole(array('where'=>array('name='=>$name,'locked!='=>10)));
				if(empty($temp)){
					$affected=$role->IRole(array('name'=>$name,'describe'=>$describe,'locked'=>$locked));
					if($affected==1){
						$output['result']=2;
						$output['msg']='添加成功.';
					}else{
						$output['msg']='您输入的数据有误.';
					}
				}else{
					$output['msg']='您输入的角色名称已存在.';
				}
			}
			header('Content-type:application/json');
			echo json_encode($output);
    	}else{
    		$this->yum->view('append.php');
    	}
	}
	function purview(){
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$session=$this->yum->library('session');
			$modifi=$this->yum->post('purview');$role=intval($this->yum->post('role'));$member=intval($session->GVAR('member'));
			if($role==1){
				$output['msg']='超级管理员不允许修改.';
			}else{
				if(!empty($modifi)){
					$menu=$this->yum->model('menu');
					$modifi=$menu->GMenu(array('where'=>array('id IN'=>$modifi,'method !='=>'')));
				}else{$modifi=array();}
				$temp=$modifi;$modifi=array();
				foreach($temp as $key=>$value){
					$modifi[]=array($role,$value['controller'],$value['method'],$member);
				}
				/******************默认权限***************************/
				$modifi[]=array($role,'manage','index',$member);
				$modifi[]=array($role,'manage','out',$member);
				$purview=$this->yum->model('purview');
				$purview->DPurview(array('where'=>array('role='=>$role)));
				$affected=$purview->IPurview(array('field'=>array('role','controller','method','creator'),'value'=>$modifi));
				if($affected==count($modifi)){
					$output['result']=1;$output['msg']='权限修改成功.';
				}else{
					$output['msg']='服务器忙,请稍后提交.';
				}
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$role=intval($this->yum->get('role'));
			$current=$this->yum->model('purview')->GPurview(array('where'=>array('role ='=>$role)));
			$this->yum->set(array('current'=>$current,'role'=>$role));
			$this->yum->view('purview.php');
		}
	}
	function edit(){
		$role=$this->yum->model('role');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=intval($this->yum->post('role',true));$name=$this->yum->post('name',true);$describe=$this->yum->post('describe',true);
			if(strlen($name)<=0){
				$output['msg']='名称不可为空.';
			}else{
				$affected=$role->URole(array('where'=>array('id='=>$id),'set'=>array('name ='=>$name,'`describe` ='=>$describe)));
				if($affected==1){
					$output['result']=2;
					$output['msg']='修改成功.';
				}else{
					$output['msg']='请误提交相同或未经修改的数据.';
				}
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$role=$this->yum->model('role');$current=intval($this->yum->get('role',true));
			$data['role']=$role->GRole(array('where'=>array('id ='=>$current)));
			$data['role']=reset($data['role']);
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
	}
	function locked(){
		$output=array('result'=>0,'msg'=>'','offset'=>0,'data'=>array());
		$role=$this->yum->model('role');
		$id=$this->yum->get('role');
		$locked=intval($this->yum->get('locked',true));
		if($id==1){
				$output['msg']='超级管理员不允许修改.';
		}else{
			$affected=$role->URole(array('where'=>array('id='=>$id),'set'=>array('locked='=>$locked)));
			if($affected==1){
				$output['result']=5;
				$output['td']=array('offset'=>4,'data'=>array($locked==0?'正常':'禁止'));
				$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?2:0).'}'),'text'=>($locked==0?'禁止':'激活'));
			}else{$output['msg']='成员状态已变更.';}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>